import { Plugin, Component } from 'vue'

interface ComponentModule {
  default: Component
}

const register: Plugin = {
  install(app) {
    const modules = import.meta.globEager<ComponentModule>('./**/*')
    const reg = /app(-\w+)+/g
    const components = Object.keys(modules).filter(item => reg.test(item))

    components.forEach(item => {
      const matched = item.match(reg)
      if (!matched) return

      const componentName = matched[0]

      app.component(componentName, modules[item].default)
    })
  }
}

export default register
